// pages/musci/musci.js
Page({

  /**
   * 页面的初始数据
   * 有bug,未解决
   * 音乐总长度在初始化时获取不到,之后正常显示
   * 音乐播放到一半暂停(疑似音乐资源的问题,一些音乐是vip有试听时长?)
   * 进度条时不时异常(应该是没设置归零)
   * 有空修改 2024/10/12/17:22
   */
  data: {
    sid: "s1",
    num: "",
    flag: true,
    touchS: [0, 0],
    touchE: [0, 0],
    max: 180,
    maxs: "03:00",
    time: "00:00",
    count: 0,
    listen: '1',
    musci: "",
    stop : "",
    mlist: [{
        mname: "In My Room",
        msinger: "キツネDJ",
        mmaker: "In My Room (feat. KMNZ LIZ)",
        srcs: "https://webfs.kugou.com/202410121516/dd3f254294cbb0836bc54770dc2e758a/v3/b847b9a8d53deec9a13b576dbcb93a15/yp/full/ap1014_us0_mii0w1iw8z2ai2iphcu80ooo2ki81120_pi406_mx321148121_s1916464571.mp3",
        images: "https://imgessl.kugou.com/stdmusic/20210615/20210615052701467325.jpg"
      },
      {
        mname: "LOVE 2000",
        msinger: "遠野ひかる、",
        mmaker: "LOVE 2000",
        srcs: "https://webfs.kugou.com/202410121519/eb97a3e1b2eb63b9b75b2f84308720c7/v3/31f776a095c8a596301166f4642e1769/yp/full/ap1014_us0_mii0w1iw8z2ai2iphcu80ooo2ki81120_pi406_mx652173050_s2517061288.mp3",
        images: "https://imgessl.kugou.com/stdmusic/20240702/20240702204402976971.jpg"
      },
      {
        mname: "Love Me Again",
        msinger: "John Newman",
        mmaker: "Love Me Again",
        srcs: "https://webfs.kugou.com/202410121524/4c7a6e1dc1bee2f4bc2ae9f056c3ab65/v3/509081d94ac00ada2d52a7827676ff77/yp/p_0_960173/ap1014_us0_mii0w1iw8z2ai2iphcu80ooo2ki81120_pi406_mx61740748_s1922558286.mp3",
        images: "https://imgessl.kugou.com/stdmusic/20150718/20150718072738205991.jpg"
      },
      {
        mname: "我和你",
        msinger: "唐宁",
        mmaker: "我和你",
        srcs: "https://webfs.kugou.com/202410121702/b454949b4d77a920e509c67df7bcbc30/v3/1ad9c7d07d02da248871be35dc3bad15/yp/p_0_480724/ap1014_us0_mii0w1iw8z2ai2iphcu80ooo2ki81120_pi406_mx312030770_s29103370.mp3",
        images: "https://imgessl.kugou.com/stdmusic/20240315/20240315170602520373.jpg"
      },
      {
        mname: "空中散步",
        msinger: "久石让",
        mmaker: "宫崎骏",
        srcs: "https://webfs.kugou.com/202410121514/cc9fcf268e51d9986a93c9d3418cc19f/v3/9feacd27467a8c81ccf1904833f67dbe/yp/full/ap1014_us0_mii0w1iw8z2ai2iphcu80ooo2ki81120_pi406_mx108808936_s2073210118.mp3",
        images: "https://imgessl.kugou.com/stdmusic/20230225/20230225071410674711.jpg"
      },
    ]
  },
  scrolls(e) {
    // this.setData({sid:"s2"})
    // console.log(e.detail)

  },
  resume(){
    var minute = parseInt(this.data.musci.currentTime / 60);
    var times = parseInt(this.data.musci.currentTime % 60);
    if (minute < 10) {
      minute = "0" + minute;
    }
    if (times < 10) {
      times = "0" + times;
    }
    this.setData({
      time: minute + ":" + times
    ,count:parseInt(this.data.musci.currentTime)})
  },
  taps(e) {
    this.setData({
      sid: e.currentTarget.dataset.id
    });
  },
  flags() {
    
   
    this.setData({
      flag: !this.data.flag
    });
    console.log(this.data.flag);
    var that = this;
    if(this.data.flag){
      this.data.musci.play();
      this.data.stop = setInterval(function(){
        that.resume();
      },1000);
     
    }else{
      clearInterval(this.data.stop);
      this.data.musci.pause();
    }
  },
  listens(e) {
    this.setmusic(e.currentTarget.dataset.id);
    this.setData({
      listen: e.currentTarget.dataset.id
    });
    var minute = parseInt(this.data.max / 60);
    var times = parseInt(this.data.max % 60);
    if (minute < 10) {
      minute = "0" + minute;
    }
    if (times < 10) {
      times = "0" + times;
    }
    this.setData({
      maxs: minute + ":" + times
    })
    console.log(e.currentTarget.dataset.id);
  },
  

  touchStart(e) {
    // console.log(e.touches[0].pageX)
    let sx = e.touches[0].pageX
    let sy = e.touches[0].pageY
    this.data.touchS = [sx, sy]
  },
  touchMove(e) {
    let sx = e.touches[0].pageX;
    let sy = e.touches[0].pageY;
    this.data.touchE = [sx, sy]
  },
  touchEnd(e) {

    let start = this.data.touchS
    let end = this.data.touchE
    // console.log(start)
    // console.log(end)
    if (start[0] < end[0] - 30 && start[1] > end[1] - 50) {
      //   console.log('右滑');
      if (this.data.sid === "s3") {
        this.setData({
          sid: "s2"
        });
      } else if (this.data.sid === "s2") {
        this.setData({
          sid: "s1"
        });
      }

    } else if (start[0] > end[0] + 30 && start[1] < end[1] + 50) {
      //   console.log('左滑');

      if (this.data.sid === "s1") {
        this.setData({
          sid: "s2"
        });
      } else if (this.data.sid === "s2") {
        this.setData({
          sid: "s3"
        });
      }
    } else if (start[1] < end[1] - 5 || start[1] > end[1] + 5) {
      // console.log('上下滑动')
    } else {
      // console.log('静止')
      this.setData({
        sid: this.data.sid
      });
    }
  },
  slides(e) {
    this.data.musci.seek(parseInt(e.detail.value))

    var minute = parseInt(e.detail.value / 60);
    var times = parseInt(e.detail.value % 60);
    if (minute < 10) {
      minute = "0" + minute;
    }
    if (times < 10) {
      times = "0" + times;
    }
    this.setData({
      time: minute + ":" + times
    , 
      count: e.detail.value
    })
  },
  empty() {


  },
  setmusic(number){
    
    // 创建BackgroundAudioManager实例
    var audio = wx.getBackgroundAudioManager()
    // 当开始播放音乐时，输出调试信息
    audio.onPlay(function () {
      console.log('开始播放')
      
    })
     // 设置背景音频的资源地址
     audio.src = this.data.mlist[number-1].srcs
    // 设置背景音频的标题
    audio.title = this.data.mlist[number-1].mname
    //歌手
    audio.singer = this.data.mlist[number-1].msinger
    //专辑
    audio.epname = this.data.mlist[number-1].mmaker
    //封面
    audio.coverImgUrl = this.data.mlist[number-1].images
    //分享地址
    audio.webUrl = this.data.mlist[number-1].srcs
   
    //设置最大长度
    console.log("x"+ audio.duration);
    console.log("x"+ this.data.mlist[number-1].srcs);
    this.setData({max: parseInt(audio.duration)});
    
     
    //进度
    var that = this;
    this.data.stop = setInterval(function(){
      that.resume();
    },1000);
    //存储对象
    this.data.musci = audio;
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setmusic(1);
    this.setData({
      listen: "1"
    });
    var minute = parseInt(this.data.max / 60);
    var times = parseInt(this.data.max % 60);
    if (minute < 10) {
      minute = "0" + minute;
    }
    if (times < 10) {
      times = "0" + times;
    }
    this.setData({
      maxs: minute + ":" + times
    })

    //音乐播放
    // const innerAudioContext = wx.createInnerAudioContext()
    // innerAudioContext.autoplay = true
    // innerAudioContext.src = 'https://webfs.kugou.com/202410121519/eb97a3e1b2eb63b9b75b2f84308720c7/v3/31f776a095c8a596301166f4642e1769/yp/full/ap1014_us0_mii0w1iw8z2ai2iphcu80ooo2ki81120_pi406_mx652173050_s2517061288.mp3'
    // innerAudioContext.loop = true
    // this.setData({max: innerAudioContext.duration});
    
    // innerAudioContext.onPlay(() => {
    //   console.log('开始播放')
    // })
    // innerAudioContext.onError((res) => {
    //   console.log(res.errMsg)
    //   console.log(res.errCode)
    // })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})